package com.yanqu.road.dao.impl.cn.activity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.impl.TempNoPoolDao;

import java.sql.CallableStatement;
import java.sql.ResultSet;

public class CnActivityDaoNotPoolImpl extends TempNoPoolDao {

    public CnActivityDaoNotPoolImpl(String dbParam) {
        super(dbParam);
    }

    /**
     * 活动信息
     */
    public JSONArray getCnActivityRankList(int activityId, int conditionType) {
        ResultSet rs = null;
        CallableStatement call = null;
        JSONArray jsonArray = new JSONArray();
        String sql = "select userId,value from t_c_activity_new_rank where activityId = " + activityId + " and conditionType = " + conditionType + " and rank <= 100 order by rank asc";
        try {
            call = getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                JSONObject jsonObject = new JSONObject();
                long userId = rs.getLong("userId");
                String value = rs.getString("value");
                if (userId > 0) {
                    jsonObject.put("userId", userId);
                    jsonObject.put("value", value);
                    jsonArray.add(jsonObject);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            closeConnection(call, rs);
        }
        return jsonArray;
    }

    /**
     * 获取活动分数
     * @param platformUserId
     * @param activityId
     * @param conditionType
     * @return
     */
    public JSONObject getActivityScore(String platformUserId, int activityId, int conditionType) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select userId,value,nickName from t_u_activity_condition where userId in (select userId from t_u_user where platformUserId = \"" + platformUserId + "\") and activityId = " + activityId + " and type  = " + conditionType;
        try {
            call = getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                JSONObject jsonObject = new JSONObject();
                long userId = rs.getLong("userId");
                String value = rs.getString("value");
                String nickName = rs.getString("nickName");
                jsonObject.put("userId", userId);
                jsonObject.put("value", value);
                jsonObject.put("nickName", nickName);
                return jsonObject;

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            closeConnection(call, rs);
        }
        return new JSONObject();
    }
}
